

import 'package:common_utils/common_utils.dart';
import 'package:flui/flui.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/router/routes.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/select_type_page.dart';
import 'package:nav_router/nav_router.dart';

class RegisterViewModel extends BaseViewModel {
  TextEditingController phoneControll = TextEditingController();
  TextEditingController pswControll = TextEditingController();
  TextEditingController codeControll = TextEditingController();
  TextEditingController invitationCodeControll = TextEditingController();
  bool pswObscureText;
  bool isInput;
  TimerUtil timerCountDown;
  int timeCount = 0;
  RegisterViewModel() {
    this.pswObscureText = true;
    this.isInput = false;
    timerCountDown = TimerUtil(mInterval: 1000, mTotalTime: 60 * 1000);
  }
  void onChanged() {
    checkInput();
  }

  void clearPhoneText() {
    this.phoneControll.clear();
    checkInput();
    notifyListeners();
  }

  void clearPswText() {
    this.pswControll.clear();
    checkInput();
    notifyListeners();
  }

  void clearCodeText() {
    this.codeControll.clear();
    checkInput();
    notifyListeners();
  }

  void clearInvitationCodeText() {
    this.invitationCodeControll.clear();
    checkInput();
    notifyListeners();
  }

  void onPswObscureText() {
    this.pswObscureText = !pswObscureText;

    notifyListeners();
  }

  void checkInput() {
    if (this.phoneControll.text.isNotEmpty &&
        this.pswControll.text.isNotEmpty &&
        this.codeControll.text.isNotEmpty) {
      this.isInput = true;
    } else {
      this.isInput = false;
    }
    notifyListeners();
  }

  void startTimerCountDown() {
    if (phoneControll.text.isEmpty) {
      FLToast.showText(text: '请输入手机号');

      return;
    }
    var dismiss = FLToast.loading(text: '正在获取验证码...');
    Http.instance
        .post('member/yzm', {'phone': phoneControll.text, 'type': 'register'},
            onSuccess: (data) {
      dismiss();
      FLToast.showSuccess(text: '验证码已发送,请注意查收');
      timerCountDown = TimerUtil(mInterval: 1000, mTotalTime: 60 * 1000);
      timerCountDown.setOnTimerTickCallback((int value) {
        double tick = (value / 1000);
        this.timeCount = tick.toInt();
        notifyListeners();
        // LogUtil.e("CountDown: " + tick.toInt().toString());

        // LogUtil.e("---------------- TimerUtil en ----------------\n");
      });
      timerCountDown.startCountDown();

      timerCountDown.startTimer();
    }, onError: (error) {}, onFail: (error) {});
  }

  void onRegister(BuildContext context) {
    var dismiss = FLToast.loading(text: '正在注册中...');
    Http.instance.post('member/regist', {
      'phone': phoneControll.text,
      'yzm': codeControll.text,
      'pwd': pswControll.text
    }, onSuccess: (data) {
      dismiss();
    
      LogUtil.e(data);
      SpUtil.putString('phone', data['phone']);
      // SpUtil.putString('is_company', data['data']['is_company']);
      SpUtil.putString('uid', data['id']);
      SpUtil.putString('avatar', data['head']);
       SpUtil.putString('token', data['token']);
       
       routePush(SelectTypePage());
    }, onError: (error) {
      dismiss();
    }, onFail: (fail) {
      dismiss();
    });
  }
}
